<?php

/**
 * BH衣橱商品用户收藏单表model
 * @author perry
 */

namespace Home\Model;

use Think\Model;

class BHClosetFavoriteModel extends Model {


    /**
     * 初始化
     */
    function _initialize() {
        $this->tableName = 'bh_closet_commodity_favorite';
        // $this->trueTableName  = 'eosi_bh_closet_commodity_favorite';
        // $this->imgUploadType='closet';
        // $this->commodityImgUploadType='commodity';
    }
    
    
    // function getByPage($uid,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true){
    // 	$map=array();
    // 	if(ic_isPosInt($uid)){
	   //  	$map['uid']=$uid;
	   //  }
	   //  elseif (is_array($uid)) {
	   //  	$super_style_uid_str=implode(",", $uid);
	   //  	$map['_string'] = " uid in ($super_style_uid_str) ";
	   //  }

	   //  if(ic_isPosInt($type)){
	   //  	if($type!=1){ //type==1 代表全部类型，无需筛选
		  //   	$map['type']=$type;
		  //   }
	   //  }
	   //  if(ic_isPosInt($status)){
		  //   	$map['status']=$status;
	   //  }
	   //  if($name!==false){
	   //  	$namef=ic_filterStr($name);
	   //  		if(ic_checkUnEmpty($namef)){
			 //    	// $map['name']=array('exp'," LIKE (1,3,8) ");
		  //   		$map['name'] = array('like',"%$name%");
		  //   	}
	   //  }
	   //  if(!ic_isPosInt($page)){
	   //  	$page=1;
	   //  }
	   //  if(!ic_isPosInt($pageSize)){
	   //  	$pageSize=10;
	   //  }
	   //  if($pageSize>50000){
	   //  	$pageSize=50000;
	   //  }
	   //  $totalCount=$this->where($map)->count();
	   //  $debug_sql=$this->getLastSql();
	   //  $datas=array();
	   //  if(($totalCount>($page-1)*$pageSize)){
	   //  	$start= ($page-1)*$pageSize;
	   //  	if($field!==true){
	   //  		$field.=",create_time";
	   //  	}
	   //  	$datas=$this->where($map)->field($field)->limit($start,$pageSize)->select();
	   //  	foreach ($datas as $key => &$value) {
	   //  		//如果返回数据里有主图（商品和衣橱都有字段img），需要转换成图片全路径
	   //  		if(array_key_exists('img', $value)){
		  //   		$value['img']=ic_getUploadImgFullPath($this->imgUploadType,$value['img'],$value['create_time']);
		  //   	}
		  //   	//如果返回数据里有副图（商品特有字段sub_imgs），需要转换成图片全路径
		  //   	if(array_key_exists('sub_imgs', $value)){
		  //   		if(ic_checkUnEmpty($value['sub_imgs'])){
			 //    		$tmparr=explode(',',$value['sub_imgs']);
				//         foreach ($tmparr as $key2 => $value2) {
				//             $tmparr[$key2] = ic_getUploadImgFullPath($this->commodityImgUploadType,$value2,$value['create_time']);
				//         }
			 //    		$value['sub_imgs'] = $tmparr;
			 //    	}
			 //    	else {
			 //    		$value['sub_imgs'] = array();
			 //    	}
		  //   	}

	   //  	}
	   //  }
	   //  // dump($this->getLastSql());
	   //  $totalPage=floor($totalCount/$pageSize)+((($totalCount%$pageSize)==0)?0:1);
	   //  return array('totalCount'=>$totalCount,'totalPage'=>$totalPage, 'pageList'=>$datas);
    // }

    function deleteOne($uid,$id){
    	if(!ic_isPosInt($id)){
	    	throw new \Exception("id参数有误.", -73);
	    }
    	$map=array('cc_id'=>$id,'uid'=>$uid);
    	$exist=$this->where($map)->find();
    	if(!$exist){
    		throw new \Exception("待删除的收藏不存在哦,请刷新页面重试.", -78);
    	}
    	
    	//先删数据
    	$deleteRe=$this->where($map)->delete();
        return $deleteRe;
    }

    function addOne($uid,$cc_id){
    	if(!ic_isPosInt($cc_id)){
	    	throw new \Exception("cc_id参数有误.", -73);
	    }
    	$map=array('id'=>$cc_id);
    	$exist=M('bh_closet_commodity')->where($map)->find();
    	if(!$exist){
    		throw new \Exception("待收藏的数据不存在哦,请刷新页面重试.", -78);
    	}
    	if(!in_array($exist['uid'], C('super_style_uid'))){
    		throw new \Exception("收藏失败,目前暂时只能收藏百家好商品哦.", -81);
    	}
    	if($exist['status']!=1){
    		throw new \Exception("收藏失败,您收藏的商品已不存在哦，请检查.", -81);
    	}
    	$datas=array();
    	$datas['uid']=$uid;
    	$datas['cc_id']=$cc_id;
    	$exist=$this->where($datas)->find();
    	if($exist){
    		throw new \Exception("收藏失败,您已经收藏过了哦，请检查.", -81);
    	}
    	$datas['create_time']=time();
    	$newId=$this->add($datas);
        return $newId;
    }

    function getOne($uid,$cc_id){
    	if(!ic_isPosInt($cc_id)){
	    	throw new \Exception("cc_id参数有误.", -73);
	    }
	    $datas=array();
    	$datas['uid']=$uid;
    	$datas['cc_id']=$cc_id;
    	$exist=$this->where($datas)->find();
    	return $exist;
    }

    function getByUid($uid,$field="id"){
    	if(!ic_isPosInt($uid)){
	    	throw new \Exception("uid参数有误.", -73);
	    }
	    $datas=array();
    	$datas['uid']=$uid;
    	// $datas['cc_id']=$cc_id;
    	$exist=$this->where($datas)->field($field)->select();
    	return $exist;
    }

    
    

}
